package org.jsbd.boss.action;

import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.annotation.SystemControllerLog;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.common.ReturnJsonCode;
import org.jsbd.boss.dao.IBaseInfoDAO;
import org.jsbd.boss.domian.BaseInfo;
import org.jsbd.boss.domian.InterceptInfo;
import org.jsbd.boss.domian.channel.ApkSimulator;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.service.impl.BaseConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/base")
public class BaseConfigController {
	public static final Logger LOGGER = LoggerFactory.getLogger(BaseConfigController.class);

	@Autowired
	private BaseConfigService baseConfigService;


	@Resource
	private IBaseInfoDAO baseInfoDAO;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String configList(Model model) {
		return "base/configList";
	}

	// 基本配置查询展示接口
	@RequestMapping(value = "/configList", params = "json")
	@ResponseBody
	public Map<String, Object> configPageList(BaseInfo baseInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", baseInfoDAO.selectConfigList(baseInfo));
		map.put("total", baseInfoDAO.selectCount(baseInfo));
		return map;
	}

	// 跳出新增页面
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addConfig(Model model, HttpServletResponse response) {
		return "base/add";
	}

	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@SystemControllerLog(description = "基地新增") 
	public void saveConfig(BaseInfo baseInfo, HttpServletResponse response,
			HttpServletRequest request) {
		baseConfigService.insertConfig(baseInfo);
		ResponseUtils.responseSuccess(response);
	}


	@RequestMapping(value = "{id}/updateConfig")
	public String configUpdate(@PathVariable Integer id, Model model) {
		BaseInfo baseInfo = baseInfoDAO.getBaseInfoById(id);
		model.addAttribute("config", baseInfo);
		return "base/updateConfig";
	}



	@RequestMapping(value = "updateConfig", method = RequestMethod.POST)
	public void updateConfig(BaseInfo baseInfo, HttpServletResponse response) {
		try {
			baseInfoDAO.updateConfig(baseInfo);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("修改基地出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	/**
	 * 根据sp_biz_name 动态获取spBizConfig
	 * 
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "/content", method = RequestMethod.GET)
	public void findBaseInfoList(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String id = request.getParameter("defaultValues");
		StringBuffer jsonData = new StringBuffer("[");
		try {
			BaseInfo form = new BaseInfo();
			List<BaseInfo> list= baseConfigService.getAllChannelInfo();
			if (list != null && list.size() > 0) {
				for (BaseInfo baseInfo : list) {
					jsonData.append("{");
					jsonData.append("\"id\":\"" +baseInfo.getId()
							+ "\",");
					if (StringUtils.isNotBlank(id)) {
						if (id.equals(baseInfo.getId())) {
							jsonData.append("\"selected\":\"" + true + "\",");
						}
					}
					jsonData.append("\"baseName\":\"" + baseInfo.getBaseName()
							+ "\"},");
				}
				jsonData.delete(jsonData.toString().length() - 1, jsonData
						.toString().length());
			}
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		jsonData.append("]");
		ResponseUtils.renderJson(response, jsonData.toString());
	}


	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@SystemControllerLog(description = "基地删除")
	public void delete(String id, HttpServletResponse response) {
		if (StringUtils.isBlank(id) || "99".equals(id)) {
			JSONObject resultJson = new JSONObject();
			resultJson.put(ReturnJsonCode.RETURN_CODE, ReturnJsonCode.MsgCodeEnum.FAILURE.getCode());
			resultJson.put(ReturnJsonCode.RETURN_MSG, "id不能为空或者99其他");
			ResponseUtils.renderJson(response, resultJson.toString());
			return;
		}
		baseConfigService.deleteConfig(id);
		ResponseUtils.responseSuccess(response);
	}
	
	/**
	 * 获取基地
	 * 
	 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
	 * @return 基地信息
	 * 
	 */
	@RequestMapping(value = "baseCombobox", method = RequestMethod.GET)
	@ResponseBody
	public List<BaseInfo> baseCombobox () {
		List<BaseInfo> list = baseConfigService.getAllChannelInfo();
		BaseInfo baseInfo = new BaseInfo();
		baseInfo.setId("-1");
		baseInfo.setBaseName("总量");
		list.add(0, baseInfo);
		return list;
	}
	
}
